﻿using System.Security.Cryptography;
using System.Text;

namespace Swimj.Core.Utilities
{
    public static class StringHelper
    {
        public static string FirstLower(string s)
        {
            return s.Substring(0, 1).ToLower() + s.Substring(1);
        }

        public static string GetMd5(string myString)
        {
            MD5 mD = MD5.Create();
            byte[] bytes = Encoding.Unicode.GetBytes(myString);
            byte[] array = mD.ComputeHash(bytes);
            string text = null;
            for (int i = 0; i < array.Length; i++)
            {
                text += array[i].ToString("x");
            }
            return text;
        }

        public static string DefaultValue(object value)
        {
            if (value == null)
            {
                return string.Empty;
            }
            return value.ToString();
        }

        public static string DefaultValue(object target, string fieldName)
        {
            if (target == null)
            {
                return string.Empty;
            }
            object propertyValue = ObjectHelper.GetPropertyValue(target, fieldName);
            return DefaultValue(propertyValue);
        }

        public static string DefaultValue(object target, string fieldName, string format)
        {
            if (target == null)
            {
                return string.Empty;
            }
            object propertyValue = ObjectHelper.GetPropertyValue(target, fieldName);
            if (!string.IsNullOrEmpty(format))
            {
                return string.Format(format, propertyValue);
            }
            return DefaultValue(propertyValue);
        }

        public static bool IsNumber(string strVar)
        {
            int length = strVar.Length;
            for (int i = 0; i < length; i++)
            {
                char c = strVar[i];
                if (i == 0 && c == '0')
                {
                    return false;
                }
                if (c < '0' || c > '9')
                {
                    return false;
                }
            }
            return true;
        }

        public static string FirstNotEmpty(params string[] args)
        {
            if (args == null || args.Length <= 0)
            {
                return string.Empty;
            }
            foreach (string text in args)
            {
                if (!string.IsNullOrEmpty(text))
                {
                    return text;
                }
            }
            return string.Empty;
        }

        public static string SubstrAfterLast(string str, string separator)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (string.IsNullOrEmpty(separator))
            {
                return string.Empty;
            }
            int num = str.LastIndexOf(separator);
            if (num == -1 || num == str.Length - separator.Length)
            {
                return string.Empty;
            }
            return str.Substring(num + separator.Length);
        }
    }
}
